<?php

namespace app\studentLayer\controller;


use app\studentLayer\model\ClassesModel;
use app\studentLayer\model\StudentModel;
use phpDocumentor\Reflection\DocBlock\Tags\Param;
use think\Controller;
use think\Db;

class Classes extends Controller
{


    //查询班级的方法
    public function SelectClasses()
    {
//        $all =  Db::table('s_classes')->all();

        //var_dump($all);die;
//        $SelectClasses = Db::table('s_classes')->select();
//        //var_dump($SelectClasses);die;
//        $this->assign('SelectClasses',$SelectClasses);
        //渲染视图
        //json()
        return $this->fetch('ClassesView/SelectClasses');
    }


    public function AjaxSelectClasses()
    {
        $limit = $this->request->get();
//        var_dump($limit);die;


        //  $SelectClasses = Db::table('s_classes')->select();

        $SelectClasses = Db::table('s_classes')->page($limit['page'], $limit['limit'])->select();
        $count = Db::table('s_classes')->count('id');
        //var_dump($count);die;

        $this->returnAjax('0', "", $SelectClasses, $count);
    }

//    private  function type(int $art_id)
//    {
//        $artAll = Db::table('liuyan')->where(['art_id'=> $art_id])->select();
//
//        if(empty($artAll)){
//            return [];
//        }
//        $list = $this->ch($artAll[0]['id']);
//        echo json_encode($list);die;
//    }
//    private function ch($s_id , &$result = [])
//    {
//        $artAll = Db::table('liuyan')->where(['s_id'=> $s_id])->select();
//        if(empty($artAll)){
//            return [];
//        }
//
//        $list = [];
//
//        foreach ($artAll as $val){
//            $result[] = $val;
//            $list['children']=  $this->ch($val['id'],$result);
//        }
//
//        return  $list;
//    }


    //用来检查班级名字是否可用，是否重复，用来对比
    public function checkName()
    {
        //$check = $this->request->post();

        //记得进入函数底层去看看
        $check = $this->request->param('name');
        //var_dump($check);die;
        //查询是否与数据库里面的名字重复
        $status = Db::table("s_classes")->get(["name" => $check]);
       // var_dump($status);die;
        if (empty($status)) {
            return ["status" => "该名字能够使用"];
        } else {
            return ["status" => "名字重复"];
        }
    }


    //添加班级的方法
    public function InsertClasses()
    {
        if ($this->request->isPost()) {
            //通过表单传过来的数据
            $InsertClassesData = $this->request->post();
            //var_dump($InsertClassesData);die;

            $InsertClasses = Db::table('s_classes')->insert($InsertClassesData);
            if ($InsertClasses) {

                //请一定要注意返回路径
                $this->returnAjax(0, "添加成功");
//                return $this->success('添加成功','Classes/SelectClasses');
            } else {
                return $this->error('添加失败', 'Classes/SelectClasses');
            }
        } else {
            return $this->fetch('ClassesView/InsertClasses');
        }

    }


    //删除班级的方法
    public function DeleteClasses()
    {

        $DeleteClassesData = $this->request->get();
        // var_dump($DeleteClassesData);die;

//        delete from s_classes where id = 16
        $DeleteClasses = Db::table('s_classes')->delete($DeleteClassesData);

        if ($DeleteClasses) {
            //注意路径,和相关函数的使用
            return $this->returnAjax('', '删除成功');
            //return $this->success('删除成功', 'Classes/SelectClasses');

            //这是给HTML视图进行渲染的
            // return $this->fetch('/Classes/SelectClasses');
        } else {
            return $this->returnAjax('', '删除失败');
            // return $this->error('删除失败', 'Classes/SelectClasses');
        }


    }


    public function UpdateClasses()
    {
        if ($this->request->isPost()) {
            $UpdateClassesData = $this->request->post();
            $UpdateClasses = Db::table('s_classes')->where(['id' => $UpdateClassesData['id']])->find();

            $UpdateClasses = $UpdateClasses;
            //var_dump($UpdateClasses);die;
            $this->returnAjax('0', 'success', $UpdateClasses);//问题所在
        } else {
            $id = $this->request->get("id");
            $this->assign('id', $id);
            return $this->fetch('ClassesView/UpdateClasses');
        }
    }

    public function AjaxUPdate()
    {
        if ($this->request->isPost()) {

            $UpdateClassesData = $this->request->post();
            // var_dump($UpdateClassesData);die;
            $id['id'] = $UpdateClassesData['id'];
            $UpdateClasses = Db::table('s_classes')->where($id)->data(['name' => $UpdateClassesData['name']])->update();
            // var_dump($UpdateClasses);die;
            if ($UpdateClasses) {
                $this->returnAjax('0', '修改成功');
                //return $this->success('修改成功','Classes/SelectClasses');
            } else {
                return $this->error('修改失败', 'Classes/SelectClasses');
            }
        }
    }


    //返回的一个数组值
    public function returnAjax(string $code, string $msg, array $shop = [], $count = NULL)
    {

        $shop = [
            'code' => $code ? $code : 0,
            'msg' => $msg ? $msg : "fail",
            'shop' => $shop,
            'count' => $count
        ];


        // var_dump($shop);die;
        echo(json_encode($shop));
        die;
    }


}